<?php
declare (strict_types = 1);

namespace app\controller;


use think\facade\Db;


class ApiLogin extends ApiCommon
{

    //微信小程序登录
    public function login_xcx()
    {
        $code = input("post.code");
        $appid = 'wxf6fe9711e57615f7';
        $secret = "082854c920ef52e57cdaaf4db5a5a0e4";

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';

        $arrContextOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ]
        ];
        $res = file_get_contents($url, false, stream_context_create($arrContextOptions));
        $res  = json_decode($res,true);
        $userinfo = input("post.rawData");
        $userinfo = json_decode($userinfo,true);
        $data["data"] = $res;

        $data["userinfo"] = $userinfo;
        $openid = $res["openid"];
        $user = Db::name("user")->where("openid_wx",$openid)->find();

        $update["headimgurl"] = $userinfo["avatarUrl"];
        $update["nickname"] = $userinfo["nickName"];
        $update["gender"] = $userinfo["gender"];
        $update["last_time"] = time();
        $update["last_ip"] = \think\facade\Request::ip();

        if($user){
            //用户已存在  更新用户的头像及登录信息
            Db::name("user")->where("id",$user["id"])->update($update);
        }else{
            //新用户
            $update["reg_time"] = time();
            $update["reg_ip"] = \think\facade\Request::ip();
            $update["sign_time"] = time();
            $update["openid_wx"] = $res["openid"];
            $update["form"] = "微信小程序";
            $uid = Db::name("user")->insertGetId($update);
            $user =  Db::name("user")->find($uid);
        }

        //登录日志
        $log["uid"] = $user["id"];
        $log["nickname"] = $user["nickname"];
        $log["ip"] = \request()->ip();
        $log["devicename"] = "微信";
        $log["device"] = "微信小程序";
        $log["url"] = \request()->url();
        \app\common\model\UserLoginLog::create($log);

        $token = jwt_encode($user);

        return json(["data"=>$data,"user"=>$user,"token"=>$token]);
    }
    //qq小程序登录
    public function login_qq()
    {
        
        $code = input("post.code");
        $appid = 'wxf6fe9711e57615f7';
        $secret = "082854c920ef52e57cdaaf4db5a5a0e4";

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';

        $arrContextOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ]
        ];
        $res = file_get_contents($url, false, stream_context_create($arrContextOptions));
        $res  = json_decode($res,true);
        $userinfo = input("post.rawData");
        $userinfo = json_decode($userinfo,true);
        $data["data"] = $res;

        $data["userinfo"] = $userinfo;
        $openid = $res["openid"];
        $user = Db::name("user")->where("openid_wx",$openid)->find();

        $update["headimgurl"] = $userinfo["avatarUrl"];
        $update["nickname"] = $userinfo["nickName"];
        $update["gender"] = $userinfo["gender"];
        $update["last_time"] = time();
        $update["last_ip"] = \think\facade\Request::ip();

        if($user){
            //用户已存在  更新用户的头像及登录信息
            Db::name("user")->where("id",$user["id"])->update($update);
        }else{
            //新用户
            $update["reg_time"] = time();
            $update["reg_ip"] = \think\facade\Request::ip();
            $update["sign_time"] = time();
            $update["openid_wx"] = $res["openid"];
            $update["form"] = "微信小程序";
            $uid = Db::name("user")->insertGetId($update);
            $user =  Db::name("user")->find($uid);
        }

        //登录日志
        $log["uid"] = $user["id"];
        $log["nickname"] = $user["nickname"];
        $log["ip"] = \request()->ip();
        $log["devicename"] = "微信";
        $log["device"] = "微信小程序";
        $log["url"] = \request()->url();
        \app\common\model\UserLoginLog::create($log);

        $token = jwt_encode($user);

        return json(["data"=>$data,"user"=>$user,"token"=>$token]);
    }
}
